package com.jiangruan.javaApiProject.net;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Set;


/**
 * @Author 默
 * @Date 9:29 2021/12/4
 * @desc: 该类用于封装提供网络请求的功能代码，用于发起网络请求，
 * 并将网络的数据进行返回
 * @Description
 */
//迭代器用于set集合,已经数据库

public class HttpUtils {

    //该函数用于实现get请求并将请求的结果返回
    //path：请求的路径
    //result：请求的结果
    public static String get(String path){
        String line = "";//最开始空行
        String result = "";//每次读取到

        BufferedReader reader = null;
        InputStream input = null;
        InputStreamReader isr = null;
        //url: http://api.xxxx?city = &name
        try {
            //Union Resource Location //统一资源定位
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();



            //用于负责发起链接
             conn.connect();
//
            input = conn.getInputStream();
//
//            HttpC
//            //字符流：按行读
//            //字节流：按字节读
            isr = new InputStreamReader(input);
            reader = new BufferedReader(isr);

            //String line = reader.readLine();

            System.out.println("result:"+ result);
            while ((line = reader.readLine()) != null){
                result += line;
            }
            System.out.println("result"+result);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭读取流
            try {
                if (reader != null) {

                    reader.close();
                }
                if (input != null){
                    input.close();
                }
                if (isr != null){
                    isr.close();
                }
            }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        return result;
        }
    public static String post(String path, Map<String,String> header, byte[] data) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        String result = null;
        BufferedReader reader = null;
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置可以发送数据和接收数据
            conn.setDoInput(true);
            conn.setDoOutput(true);
            //请求头信息设置
            //ConntentType：application/json
            //Authorization:"Basic "+base64(xxx)
            //如何读取map数据 map ：无序 ，key不能重复 String value = get（key）
            if (!header.isEmpty()){
                Set<String> keys = header.keySet();
                for (String key : keys){
                    String value = header.get(key);
                    conn.setRequestProperty(key,value);
                }
            }
            conn.setRequestMethod("POST");

            conn.connect();

            //将请求的数据发送出去
            outputStream = conn.getOutputStream();
            outputStream.write(data);

            //接收返回的响应数据
            inputStream = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = reader.readLine()) != null){
                result += line;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (reader != null){
                    reader.close();
                }
                if (inputStream != null){
                    inputStream.close();
                }
                if (outputStream != null){
                    outputStream.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return result;
    }
}

